package com.freak.neteasecloudmusic.modules.find.recommend.songlist;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.base.BaseAbstractMvpWithPlayerActivity;
import com.freak.neteasecloudmusic.base.IActivityStatusBar;
import com.freak.neteasecloudmusic.modules.base.adapter.MainTabAdapter;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.SongListCategoryEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.all.AllClassifyActivity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.SongListFragment;
import com.freak.neteasecloudmusic.view.custom.toolbar.SimpleToolbar;
import com.freak.neteasecloudmusic.view.custom.viewpager.CustomViewPager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 歌单
 *
 * @author Administrator
 */
public class SongListActivity extends BaseAbstractMvpWithPlayerActivity<SongListPresenter> implements SongListContract.View, IActivityStatusBar, ViewPager.OnPageChangeListener {


    @BindView(R.id.simple_toolbar_song_list)
    SimpleToolbar simpleToolbarSongList;
    @BindView(R.id.slidingTabLayoutSongList)
    SlidingTabLayout slidingTabLayoutSongList;
    @BindView(R.id.constraint_layout_homepage_classify)
    ConstraintLayout constraintLayoutHomepageClassify;
    @BindView(R.id.constraintLayoutSongList)
    ConstraintLayout constraintLayoutSongList;
    @BindView(R.id.customViewPagerSongList)
    CustomViewPager customViewPagerSongList;
    private MainTabAdapter mainTabAdapter;
    private List<String> stringList;
    private List<Fragment> fragmentList;
    private boolean isScroll;
    private List<SongListCategoryEntity.SubBean> selectList;

    @Override
    protected int getLayout() {
        return R.layout.activity_song_list;
    }

    @Override
    protected void initEventAndData() {


    }

    @Override
    protected void onCreateLoadData() {

    }

    @Override
    protected void onDestroyRelease() {

    }

    @Override
    protected void onResumeLoadData() {

    }

    @SuppressLint("CheckResult")
    @Override
    protected void initView() {
        simpleToolbarSongList.setLeftIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        stringList = new ArrayList<>();
        fragmentList = new ArrayList<>();
        selectList = new ArrayList<>();
        customViewPagerSongList.addOnPageChangeListener(this);
        mPresenter.loadSongListCategory();
    }

    @Override
    protected SongListPresenter createPresenter() {
        return new SongListPresenter();
    }

    @Override
    public void showToast(String toast) {

    }

    @Override
    public int getStatusBarColor() {
        return getResources().getColor(R.color.color_fbfbfb);
    }

    @Override
    public int getDrawableStatusBar() {
        return 0;
    }


    /**
     * 歌单分类
     *
     * @param songListCategoryEntity
     */
    @Override
    public void loadSongListCategorySuccess(SongListCategoryEntity songListCategoryEntity) {
        stringList.clear();
        fragmentList.clear();
        if (songListCategoryEntity != null) {
            stringList.add("推荐");
            Bundle recommendBundle = new Bundle();
            recommendBundle.putString("name", "推荐");
            recommendBundle.putString("cat", "推荐");
            recommendBundle.putInt("type", 0);
            SongListFragment recommendSongListFragment = new SongListFragment();
            recommendSongListFragment.setArguments(recommendBundle);
            fragmentList.add(recommendSongListFragment);

            SongListCategoryEntity.SubBean recommend = new SongListCategoryEntity.SubBean();
            recommend.setName("推荐");
            selectList.add(recommend);


            stringList.add("官方");
            Bundle officialBundle = new Bundle();
            officialBundle.putString("name", "官方");
            officialBundle.putString("cat", "猜你喜欢");
            officialBundle.putInt("type", 1);
            SongListFragment officialSongListFragment = new SongListFragment();
            officialSongListFragment.setArguments(officialBundle);
            fragmentList.add(officialSongListFragment);

            SongListCategoryEntity.SubBean official = new SongListCategoryEntity.SubBean();
            official.setName("官方");
            selectList.add(official);

            stringList.add("精品");
            Bundle boutiqueBundle = new Bundle();
            boutiqueBundle.putString("name", "精品");
            boutiqueBundle.putString("cat", "全部歌单");
            boutiqueBundle.putInt("type", 2);
            SongListFragment boutiqueSongListFragment = new SongListFragment();
            boutiqueSongListFragment.setArguments(boutiqueBundle);

            SongListCategoryEntity.SubBean boutique = new SongListCategoryEntity.SubBean();
            boutique.setName("精品");
            selectList.add(boutique);


            fragmentList.add(boutiqueSongListFragment);
            if (songListCategoryEntity.getSub() != null && songListCategoryEntity.getSub().size() > 0) {
                if (songListCategoryEntity.getSub().size() > 5) {
                    for (int i = 0; i < 5; i++) {
                        stringList.add(songListCategoryEntity.getSub().get(i).getName());
                        Bundle bundle = new Bundle();
                        bundle.putString("name", songListCategoryEntity.getSub().get(i).getName());
                        bundle.putString("cat", songListCategoryEntity.getSub().get(i).getName());
                        bundle.putInt("type", 4);
                        SongListFragment songListFragment = new SongListFragment();
                        songListFragment.setArguments(bundle);
                        fragmentList.add(songListFragment);
                        selectList.add(songListCategoryEntity.getSub().get(i));
                    }
                } else {
                    for (int i = 0; i < songListCategoryEntity.getSub().size(); i++) {
                        stringList.add(songListCategoryEntity.getSub().get(i).getName());
                        Bundle bundle = new Bundle();
                        bundle.putString("name", songListCategoryEntity.getSub().get(i).getName());
                        bundle.putString("cat", songListCategoryEntity.getSub().get(i).getName());
                        bundle.putInt("type", 4);
                        SongListFragment songListFragment = new SongListFragment();
                        songListFragment.setArguments(bundle);
                        fragmentList.add(songListFragment);
                        selectList.add(songListCategoryEntity.getSub().get(i));
                    }

                }
            }
            customViewPagerSongList.setScanScroll(true);
            mainTabAdapter = new MainTabAdapter(getSupportFragmentManager(), fragmentList, stringList);
            slidingTabLayoutSongList.setViewPager(customViewPagerSongList, stringList.toArray(new String[0]), mActivity, (ArrayList<Fragment>) fragmentList);
            customViewPagerSongList.setCurrentItem(0);

        }


    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_DRAGGING:
                isScroll = false;
                break;
            case ViewPager.SCROLL_STATE_IDLE:
                if (customViewPagerSongList.getCurrentItem() == customViewPagerSongList.getAdapter().getCount() - 1 && !isScroll) {
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("list", (Serializable) selectList);
                    gotoActivity(AllClassifyActivity.class,false,bundle);
                }
                break;
            case ViewPager.SCROLL_STATE_SETTLING:
                isScroll = true;
                break;
        }
    }


    @OnClick({R.id.constraint_layout_homepage_classify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.constraint_layout_homepage_classify:
                Bundle bundle=new Bundle();
                bundle.putSerializable("list", (Serializable) selectList);
                gotoActivity(AllClassifyActivity.class,false,bundle);
                break;
        }
    }
}
